package com.taplinker.client.im.rpc;

import android.content.Context;
import android.util.Log;

import com.taplinker.client.AppConfigContext;
import com.taplinker.client.im.domain.ImDataCenter;
import com.taplinker.client.im.util.TemplateUtil;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;

public class UpdateTemplate extends SimpleBaseCommand {
	private static final String TAG = "com.taplinker.client.im.rpc.UpdateTemplate";
	private String mVersion;
	
	public UpdateTemplate(){};
	public UpdateTemplate(String version, Context context){
		super(context);
		mVersion = version;
	};
	
	@Override
	public Result go() {
		return new HttpTemplate().exchange(AppConfigContext.TAPLINKER_WEB
				+ "/services/im/v1/find/module/" + mVersion);
	}

	@Override
	public void yes(Result result) {
		Log.i(TAG, "更新模板成功，现在版本为" + mVersion);
		try {
			TemplateUtil.saveTemplate(context, (byte[])result.object);
			TemplateUtil.updateLocalVer(context, mVersion);
			ImDataCenter.getInstance().noticeListener(ImDataCenter.TEMPLATE_UPDATE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.yes(result);
	}
	
}
